package com.swfee.qinben.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.swfee.qinben.R;
import com.swfee.qinben.session.MyApplication;
import com.swfee.qinben.util.CurenActivitUtils;

public class AppBaseActivity extends Activity  {

	private ProgressDialog progressDialog;
	private Toast myToast;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		CurenActivitUtils.setCurActivity(this);
		MyApplication.getInstance().addActivity(this);
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST);

	}

	public void startActivityUndoFinish(Intent intent) {
		startActivity(intent);
		openNewAnim();
	}

	public void startActivityBeforeFinish(Intent intent) {
		finish();
		startActivity(intent);
	}

	@Override
	public void startActivityForResult(Intent intent, int requestCode) {
		super.startActivityForResult(intent, requestCode);
		openNewAnim();
	}

	@Override
	protected void onResume() {
		super.onResume();
		CurenActivitUtils.setCurActivity(this);
	}

	public void openNewAnim() {
		overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);
	}

	public void closeAnim() {
		overridePendingTransition(R.anim.push_left_in, R.anim.push_right_out);
	}

	public void closeMe() {
		finish();
		closeAnim();
	}

	public void showProgressDialog(String msg){
		if (progressDialog == null) {
			progressDialog = new ProgressDialog(this);
			progressDialog.setIcon(R.drawable.icon);
			progressDialog.setCancelable(false);
		}
		progressDialog.setMessage(msg);
		progressDialog.show();
			
	}
	
	
	
	public void hideProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
		}
	}
	
	public void showMyToast(String msg,int toastLength,int gravity ) {
		if (myToast == null) {
			LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View layout = inflater.inflate(R.layout.toast_my_define, null);
			TextView tv = (TextView) layout.findViewById(R.id.tv_toast);
			tv.setText(msg);
			myToast = Toast.makeText(this, msg,toastLength);
			if(gravity!=-1){
				myToast.setGravity(gravity, 0, 0);
			}
			myToast.setView(layout);
			myToast.show();
		} else {
			TextView tv = (TextView) myToast.getView().findViewById(R.id.tv_toast);
			tv.setText(msg);
		}
		myToast.show();
	}
	public void showMyToast(String msg,int toastLength) {
		showMyToast(msg, toastLength, -1);
	}
	
	
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 如果是返回键,直接返回到桌面

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (this instanceof BackKeyExitService) {
				CurenActivitUtils.exitDialog();
			}else{
				closeMe();
			}
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}

	}

	
	
	
	

}
